class MachinesController < ApplicationController

  before_filter :find_user,:only=>[:new,:create,:edit]
  def index

  end
  def new
    @machine=@user.machines.new do |machine|
      machine.states.push [State.new,State.new]
      machine.events.push Event.new
      machine.events.first.state_transitions.push([
          StateTransition.new do |transition|
            transition.from_state=machine.states.first
            transition.to_state=machine.states.second
          end])
    end
  end

  def create
    @machine=@user.machines.new(params[:machine])
    if  @machine.save
      redirect_to :action=>:edit,:id=>@machine.id
    else
      render :action=>:new
    end
  end

  def edit
 @machine=Machine.find(params[:id])
      @machine.events.push Event.new
      @machine.events.first.state_transitions.push([
          StateTransition.new do |transition|
            transition.from_state=@machine.states.first
            transition.to_state=@machine.states.second
          end])
  end


def update
 @machine=Machine.find(params[:id])
 @machine.update_attributes(params[:machine])
end
  private
  def find_user
    @user=User.find params[:user_id]
  end
end
